package main

import (
	"fmt"
	"reflect"
)

// rVal := reflect.TypeOf(b)
// iVal := rVal.Interface()

func testInt(v interface{}) {
	typ := reflect.TypeOf(v)
	fmt.Printf("typ的类型=%T, typ的值=%v\n", typ, typ)

	val := reflect.ValueOf(v)
	fmt.Printf("val的类型=%T, val的值=%v\n", val, val)

	i := val.Interface()
	fmt.Printf("i的类型=%T, i的值=%v\n", i, i)

	// 转为int
	//num := i.(int)
	// 或者
	num := val.Int() // 前提是确实是int类型
	fmt.Printf("num的类型=%T, num的值=%v\n", num, num)

	// 获取kind
	kind1 := typ.Kind()
	kind2 := val.Kind()
	fmt.Printf("kind1的类型=%T, kind1的值=%v\n", kind1, kind1)
	fmt.Printf("kind2的类型=%T, kind2的值=%v\n", kind2, kind2)
}

type student struct {
	name string
	age  int
}

func testStruct(v interface{}) {
	typ := reflect.TypeOf(v)
	fmt.Printf("typ的类型=%T, typ的值=%v\n", typ, typ)

	val := reflect.ValueOf(v)
	fmt.Printf("val的类型=%T, val的值=%v\n", val, val)

	i := val.Interface()
	fmt.Printf("i的类型=%T, i的值=%v\n", i, i)

	// 转为int
	stu := i.(student)
	fmt.Printf("num的类型=%T, num的值=%v\n", stu, stu)

	// 获取kind
	kind1 := typ.Kind()
	kind2 := val.Kind()
	fmt.Printf("kind1的类型=%T, kind1的值=%v\n", kind1, kind1)
	fmt.Printf("kind2的类型=%T, kind2的值=%v\n", kind2, kind2)
}

func main() {

	//i := 1
	//testInt(i)

	stu := student{"admin", 20}
	testStruct(stu)
}
